import cv2
import numpy
import copy
output_path = "/Users/hqqs/Desktop/500M/src4/"
text_path = "/Users/hqqs/Desktop/500m/data.txt"

#计算要添加的灰度数值
def calVal(bg):
    bg_var = numpy.var(bg)#方差
    bg_mean = numpy.mean(bg)#均值
    #bg_MSE = numpy.std(bg,axis=None,ddof=1)
    ex_src = 4
    obj_mean = ex_src*bg_var + bg_mean#信杂比
    #obj_LSNR = (10** (ex_src/10))*bg_MSE + bg_mean
    text_save(text_path, "背景均值-方差："+str(bg_mean)+"-"+str(bg_var))
    text_save(text_path, "目标均值：" + str(obj_mean))
    text_save(text_path, "\n")
    return obj_mean

#保存均值
def text_save(filename, data):#filename为写入文件的路径，data为要写入数据列表.
    file = open(filename,'a')
    #s = "["+str(data)+"]"
    #s = str(data[i]).replace('[','').replace(']','')#去除[],这两行按数据不同，可以选择
    #s = s.replace("'",'').replace(',','') +'\n'   #去除单引号，逗号，每行末尾追加换行符
    file.write(data)
    file.close()
    print("保存文件成功")

img = cv2.imread('/Users/hqqs/Desktop/500M-0425915.tif')
temp = cv2.imread('/Users/hqqs/Desktop/500M-0425915.tif')
length = img.shape[0]
width = img.shape[1]
#print(numpy.mean(img))
# dst = cv2.resize(img,(length//10,width//10),interpolation=cv2.INTER_CUBIC)
# temp = cv2.resize(temp,(length//10,width//10),interpolation=cv2.INTER_CUBIC)
x=5000
i = 1

while x<7300:
    y = 2*(x-200)-((x-200)**2)//400 + 63000

    if i==11:
        break
    img = copy.deepcopy(temp)
    bg = img[x-2:x+2,y-2:y+2]
    # point = "("+str(x)+","+str(y)+")"
    # text_path(text_path,point)
    obj_mean = calVal(bg)
    #obj_mean = 255
    print(x,y)
    #print(img[2000,2000])
    cv2.rectangle(img,(x,y),(x+2,y+2),(obj_mean,obj_mean,obj_mean),thickness=-1)
   # img = cv2.resize(img, (length // 10, width // 10), interpolation=cv2.INTER_CUBIC)
    cv2.imwrite(output_path+str(i)+'.png',img)
    i+=1
    #cv2.imshow("window",img)
    x+=10
cv2.waitKey(0)
